package cn.signalpha.modules.base.controller.admin;

import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.base.dto.ContactDTO;
import cn.signalpha.modules.base.entity.Contact;
import cn.signalpha.modules.base.service.ContactService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_BASE_NAME + "/admin/contact")
@Api(value = "联系方式", tags = "联系方式")
public class ContactController extends BaseController {

	private final ContactService contactService;

    @GetMapping("/page")
    @ApiOperation(value = "分页", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name", dataType = "string", value = "名称"),
    })
    public R<IPage<Contact>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
        QueryWrapper<Contact> queryWrapper = Condition.getQueryWrapper(params, Contact.class);
        IPage<Contact> pages = contactService.page(Condition.getPage(query), queryWrapper);
        return R.data(pages);
    }

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<Contact> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		Contact detail = contactService.getById(id);
		return R.data(detail);
	}

	@PostMapping("/submit")
	@ApiOperation(value = "修改", notes = "传入contact")
	public R submit(@Valid @RequestBody ContactDTO contact) {
		contactService.removeCaChe();
		return R.status(contactService.saveOrUpdate(BeanUtil.copy(contact, Contact.class)));
	}

	@PostMapping("/remove")
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		contactService.removeCaChe();
		return R.status(contactService.deleteLogic(Func.toLongList(ids)));
	}

}
